package demo1.service;

import demo1.dao.Mapper.goodsMapper;
import demo1.dao.Mapper.userMaiMapper;
import demo1.dao.sql.goodssql;
import demo1.dao.sql.userMaisql;
import demo1.pojo.goods;
import demo1.pojo.userMai;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class userMaiService {
    public List<goods> addGoods(int price,String goodsMessage,String goodsName,String goodsType,int goodsNum,int id)throws Exception{//添加商品
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.addGoods(price,goodsMessage,goodsName,goodsType,goodsNum,id);//添加商品的时候，默认为下架状态
        List<goods> goodsList;
        goodsList = goodsMapper.selectById(id);//查询用户对应的商品
        return goodsList;
    }
    public void deleteGoods(int id,int goodsID)throws Exception{//删除商品
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.deleteGoods(id,goodsID);//删除商品
    }
    public goods modifyGoods(int id,int goodsID)throws Exception {//显示商品的信息
        goodsMapper goodsMapper = new goodssql();
        ResultSet rs = goodsMapper.selectByAll(id, goodsID);
        goods good = null;
        if (rs.next()) {
            int price = rs.getInt("price");//价格
            good = new goods();
            good.setPrice(price);
            good.setGoodsMessage(rs.getString("goodsMessage"));
            good.setGoodsName(rs.getString("goodsName"));
            good.setGoodsID(rs.getInt("goodsID"));
        }
        return good;
    }
    public goods modifyGoodsNum1(int id,int goodsID)throws Exception{//查看商品库存
        goodsMapper goodsMapper = new goodssql();//调用接口
        goods good = null;
        ResultSet rs = goodsMapper.selectByAll(id, goodsID);//查询用户对应的商品
        if (rs.next()) {
            int goodsXiaoLiang = rs.getInt("goodsXiaoLiang");
            int price = rs.getInt("price");
            int goodsNum = rs.getInt("goodsNum");
            String goodsName = rs.getString("goodsName");
            String goodsMessage = rs.getString("goodsMessage");
            good = new goods();
            good.setGoodsID(goodsID);
            good.setGoodsName(goodsName);
            good.setGoodsMessage(goodsMessage);
            good.setGoodsNum(goodsNum);
            good.setPrice(price);
            good.setGoodsXiaoLiang(goodsXiaoLiang);
        }
        return good;
    }
    public void modifyGoodsNum2(int goodsNum,int id,int goodsID)throws Exception{//修改商品库存
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.updateGoodsNum(goodsNum,id,goodsID);
    }
    public int checkGoodsNum(int goodsID)throws Exception{//查看库存是不是为0
        goodsMapper goodsMapper = new goodssql();
        goods goods = new goods();
        goods = goodsMapper.selectByGoodsId(goodsID);
        int goodsNum = goods.getGoodsNum();
        if(goodsNum == 0){
            //没有库存不能上架
            return 0;
        }
        else{
            return 1;
        }
    }
    public void updateGoodsStatue(String statue,int id,int goodsID)throws Exception{//更新商品状态
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.updateGoodsStatue(statue,id,goodsID);
    }
    public void updateGoodsTime(int id,int goodsID)throws Exception{//更新商品上架时间
        goodsMapper goodsMapper = new goodssql();
        Date date = new Date();
        long timestamp = date.getTime();
        Timestamp time = new Timestamp(timestamp);
        goodsMapper.updateGoodsTime(time,id,goodsID);
    }
    public List<goods> showgoods(int id)throws Exception{//展示所有的商品
        List<goods> goodsList;
        goodsMapper goodsMapper = new goodssql();//调用接口
        goodsList = goodsMapper.selectById(id);//查询用户对应的商品
        return goodsList;
    }
    public void updateGoodsName(String goodsName,int id,int goodsID)throws Exception{//更新商品名称
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.updateGoodsName(goodsName,id,goodsID);
    }
    public void updateGoodsMessage(String goodsMessage,int id,int goodsID)throws Exception{//更新商品信息
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.updateGoodsMessage(goodsMessage,id,goodsID);
    }
    public void updateGoodsType(String goodsType,int id,int goodsID)throws Exception{//更新商品种类
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.updateGoodsType(goodsType,id,goodsID);
    }
    public void updateGoodsprice(int price,int id,int goodsID)throws Exception{//更新商品价格
        goodsMapper goodsMapper = new goodssql();
        goodsMapper.updateGoodsprice(price,id,goodsID);
    }
    public int checkLogin(String username,String password)throws Exception{//检查有没有这个用户
        userMaiMapper userMaiMapper = new userMaisql();
        ResultSet rs = userMaiMapper.selectAll(username,password);
        if(rs.next()){
            return 1;//如果存在返回1
        }
        else {
            return 0;//如果不存在返回0
        }
    }
    public userMai write(String username,String password)throws Exception {//写入用户
        userMaiMapper userMaiMapper = new userMaisql();
        ResultSet rs = userMaiMapper.selectAll(username, password);
        userMai users = null;
        if (rs.next()) {
            users = new userMai();
            users.setUsername(username);
            users.setPassword(password);
            users.setId(rs.getInt("id"));
            users.setUserAddress(rs.getString("userAddress"));
            users.setUserDo(rs.getString("userDo"));
        }
        return users;
    }
    public void updateUserMaiName(String username,int id)throws Exception{//更新用户名
        userMaiMapper userMaiMapper = new userMaisql();
        userMaiMapper.updateUserMaiName(username, id);
    }
    public void updateUserMaiAddress(String userAddress,int id)throws Exception{//更新地址
        userMaiMapper userMaiMapper = new userMaisql();
        userMaiMapper.updateUserMaiAddress(userAddress, id);
    }
    public void updateUserMaiDo(String userDo,int id)throws Exception{//更新用户主营业务
        userMaiMapper userMaiMapper = new userMaisql();
        userMaiMapper.updateUserMaiDo(userDo, id);
    }
    public void updateUserMaiPwd(String userPwd,int id)throws Exception{//更新密码
        userMaiMapper userMaiMapper = new userMaisql();
        userMaiMapper.updateUserMaiPwd(userPwd,id);//设置新密码
    }
    public int checkName(String userName)throws Exception{//检查有没有相同的用户名
        userMaiMapper userMaiMapper = new userMaisql();
        ResultSet rs = userMaiMapper.selectAll();
        while (rs.next()) {
            if(Objects.equals(userName, rs.getString("userName"))){
                //如果有相同的用户名
                return 0;
            }
        }
        return 1;
    }
    public void insertUserMai(String userName,String userPwd)throws Exception{//实现注册
        userMaiMapper userMaiMapper = new userMaisql();
        userMaiMapper.insertUserMai(userName,userPwd);//实现注册
    }
    public String checkGoodsStatue(int id,int goodsID)throws Exception{
        goodsMapper goodsMapper = new goodssql();
        ResultSet rs = goodsMapper.selectByAll(id,goodsID);
        if(rs.next()){
            return rs.getString("statue");
        }
        else {
            return null;
        }
    }
}
